package com.kuhyygy.sqlite.provider;

import com.kuhyygy.sqlite.dao.DBHelper;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;

/**
 * ContentProvider
 *     提供服务供外部应用调用
 * @author KuhYygy
 * @since 2014-03-22
 */
public class UserContentProvider extends ContentProvider {
	
	private DBHelper dbHelper;
	private SQLiteDatabase db;
	private static final int USERS = 1;
	private static final int USER = 2;
	private static final String AUTHORITY = "com.kuhyygy.sqlite.provider.userContentProvider";
	
	/* UriMathcer匹配调用者的Uri信息 */
	private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
	static {
		matcher.addURI(AUTHORITY, "user", USERS);
		matcher.addURI(AUTHORITY, "user/#", USER);
	}
	

	@Override
	public int delete(Uri uri, String arg1, String[] arg2) {
		db = dbHelper.getWritableDatabase();
		int type = matcher.match(uri);
		switch (type) {
		case USER:
			long id = ContentUris.parseId(uri);
			return db.delete("t_user", "id = ?", new String[]{String.valueOf(id)});
		case USERS:
			return db.delete("t_user", arg1, arg2);
		default:
			break;
		}
		return 0;
	}

	@Override
	public String getType(Uri arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues cv) {
		int type = matcher.match(uri);
		db = dbHelper.getWritableDatabase();
		switch (type) {
		case USER:
			long id = db.insert("t_user", "name", cv);
			return ContentUris.withAppendedId(uri, id);
		case USERS:
			long multiId = db.insert("t_user", "name", cv);
			String path = uri.toString();
			return Uri.parse(path.substring(0, path.lastIndexOf("/") + 1) + multiId);
		default:
			throw new IllegalArgumentException("Uri illegal:" + uri.toString());
		}
	}

	@Override
	public boolean onCreate() {
		try {
			dbHelper = new DBHelper(getContext());
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	@Override
	public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
			String arg4) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(Uri uri, ContentValues cv, String arg2, String[] arg3) {
		db = dbHelper.getWritableDatabase();
		switch (matcher.match(uri)) {
		case USER:
			long id = ContentUris.parseId(uri);
			String where = TextUtils.isEmpty(arg2)? "id = ?" : arg2 + " and id = ?";
			String[] params = new String[]{String.valueOf(id)};
			if (!TextUtils.isEmpty(arg2) && null != arg3) {
				params = new String[arg3.length + 1];
				for (int i = 0; i<arg3.length; i++) {
					params[i] = arg3[i];
				}
				params[arg3.length + 1] = String.valueOf(id);
			}
			return db.update("t_user", cv, where, params);
		case USERS:
			return db.update("t_user", cv, arg2, arg3);
		default:
			break;
		}
		return 0;
	}

}
